package kr.or.kead.ui.meun;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;

import kr.or.kead.domain.Auth;
import kr.or.kead.service.DaoAuth;
import kr.or.kead.service.DaoInfoStudent;
import kr.or.kead.service.DaoTable;

public class passwdCheck extends JDialog implements ActionListener {

	private final JPanel contentPanel = new JPanel();
	private JPasswordField txtpassold;
	private JLabel passoldl;
	private JLabel newpasscl;
	private JPasswordField txtnewpass;
	private JPasswordField txtnewpassc;
	private JButton okButton;
	private JButton cancelButton;
	private DaoTable daostd;
	private DaoAuth daoAuth;
	private String email;
	private String pass;
	private int level;                                                     

	
	public passwdCheck(Auth auth) {
		init();
		daoAuth = new DaoAuth();
		daostd = new DaoInfoStudent();
		email = auth.getEmail();
		level = auth.getLevel();
		pass = daoAuth.selectPasswdByEmail(email);
	}

	private void init() {
		setBounds(100, 100, 450, 175);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new GridLayout(3, 3, 5, 0));
		{
			JLabel lblNewLabel = new JLabel("기존비밀번호");
			contentPanel.add(lblNewLabel);
		
			txtpassold = new JPasswordField();
		
			txtpassold.addFocusListener(new CheckedOldpass());
			
			contentPanel.add(txtpassold);
		
			passoldl = new JLabel();
			contentPanel.add(passoldl);
		
			JLabel lblNewLabel_2 = new JLabel("새로운 비밀번호");
			contentPanel.add(lblNewLabel_2);
		
			txtnewpass = new JPasswordField();
			contentPanel.add(txtnewpass);
		
			JLabel newpassrisk = new JLabel("강도");
			contentPanel.add(newpassrisk);
		
			JLabel lblNewLabel_4 = new JLabel("새비밀번호확인");
			contentPanel.add(lblNewLabel_4);
		
			txtnewpassc = new JPasswordField();
			txtnewpassc.addFocusListener(new CheckedNewpass());
			contentPanel.add(txtnewpassc);
		
			newpasscl = new JLabel();
			contentPanel.add(newpasscl);
		
			JPanel buttonPane = new JPanel();
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			buttonPane.setLayout(new GridLayout(0, 2, 0, 0));
			{
				okButton = new JButton("OK");
				okButton.addActionListener(this);
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);

				cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(this);
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == cancelButton) {
			cancelButtonActionPerformed(arg0);
		}
		if (arg0.getSource() == okButton) {
			okButtonActionPerformed(arg0);
		}
	}
	
	protected void okButtonActionPerformed(ActionEvent arg0) {
		char[] passArray = txtnewpassc.getPassword();
		int result=daostd.updataTableByEmail(String.valueOf(passArray), email);
		if(result == 0){
			JOptionPane.showMessageDialog(null, "변경완료");
		}else{
			JOptionPane.showMessageDialog(null, "변경실패");
		}
		dispose();
	}
	
	protected void cancelButtonActionPerformed(ActionEvent arg0) {
		dispose();
	}
	
	class CheckedOldpass extends FocusAdapter {
		@Override
		public void focusLost(FocusEvent e) {
			super.focusLost(e);
			if(Arrays.equals(txtpassold.getPassword(),pass.toCharArray())){
				passoldl.setText("일치");
				txtnewpass.requestFocus();
			}else{
				passoldl.setText("불일치");
			}
		}
	}
	class CheckedNewpass extends FocusAdapter {

		@Override
		public void focusLost(FocusEvent e) {
			super.focusLost(e);
			if(Arrays.equals(txtnewpass.getPassword(),txtnewpassc.getPassword())){
				newpasscl.setText("일치");
				txtnewpass.requestFocus();
			}else{
				newpasscl.setText("불일치");
			}
		}

	}
}
